package com.ruoyi.business.service.impl;

import java.io.FileNotFoundException;
import java.util.List;

import com.ruoyi.business.domain.DefinitionInfo;
import com.ruoyi.business.service.IProcessService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DefinitionDetailMapper;
import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程定义明细Service业务层处理
 *
 * @author wolfcode
 * @date 2021-07-19
 */
@Service
public class DefinitionDetailServiceImpl implements IDefinitionDetailService {
    @Autowired
    private DefinitionDetailMapper definitionDetailMapper;
    @Autowired
    private IProcessService processService;

    /**
     * 查询流程定义明细
     *
     * @param definitionInfoId 流程定义明细ID
     * @return 流程定义明细
     */
    @Override
    public DefinitionDetail selectDefinitionDetailById(Long definitionInfoId) {
        return definitionDetailMapper.selectDefinitionDetailById(definitionInfoId);
    }

    /**
     * 查询流程定义明细列表
     *
     * @param definitionDetail 流程定义明细
     * @return 流程定义明细
     */
    @Override
    public List<DefinitionDetail> selectDefinitionDetailList(DefinitionDetail definitionDetail) {
        List<DefinitionDetail> definitionDetailList = definitionDetailMapper.selectDefinitionDetailList(definitionDetail);
        // 查询出对应的描述、版本、业务key
        for (DefinitionDetail detail : definitionDetailList) {
            ProcessDefinition processDefinition = processService.selectProcessDefinitionByDeploymentId(detail.getDeploymentId());
            detail.setVersion(processDefinition.getVersion());// 部署的版本
            detail.setDescription(processDefinition.getDescription());// 部署的描述信息
            detail.setDeployKey(processDefinition.getKey());// 部署的key
        }
        return definitionDetailList;
    }

    /**
     * 新增流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int insertDefinitionDetail(DefinitionDetail definitionDetail) {
        return definitionDetailMapper.insertDefinitionDetail(definitionDetail);
    }

    /**
     * 修改流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int updateDefinitionDetail(DefinitionDetail definitionDetail) {
        return definitionDetailMapper.updateDefinitionDetail(definitionDetail);
    }

    /**
     * 删除流程定义明细对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailByIds(String ids) {
        return definitionDetailMapper.deleteDefinitionDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程定义明细信息
     *
     * @param definitionInfoId 流程定义明细ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailById(Long definitionInfoId) {
        return definitionDetailMapper.deleteDefinitionDetailById(definitionInfoId);
    }


    @Override
    @Transactional
    public void deployProcessDefinition(Long definitionId, String filePath) throws FileNotFoundException {
        //Activiti7流程部署
        Deployment deployment = processService.deploy(filePath);
        //插入流程定义明细
        DefinitionDetail detail = new DefinitionDetail();
        DefinitionInfo definitionInfo = new DefinitionInfo();
        definitionInfo.setId(definitionId);
        detail.setDefinitionInfo(definitionInfo);
        detail.setDeploymentId(deployment.getId());
        definitionDetailMapper.insertDefinitionDetail(detail);
    }
}
